package your.packag;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity implements OnClickListener {
	
	ProgressDialog dialog;
	TextView textname;
	TextView textusername;
	TextView textemail;
	TextView textpasswd;
	TextView textrepasswd;
	
	EditText editname;
	EditText editusername;
	EditText editemail;
	EditText editpasswd;
	EditText editrepasswd;
	
	Button register;	
	TextView back;
	
	List<Cookie> cookies;
	 private Handler handler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	        	dialog.dismiss();
	        }
	 };
	
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.register);
	        
	        editname = (EditText)findViewById(R.id.registereditText1);
	        editusername = (EditText)findViewById(R.id.registereditText2);
	        editemail = (EditText)findViewById(R.id.registereditText3);
	        editpasswd = (EditText)findViewById(R.id.registereditText4);
	        editrepasswd = (EditText)findViewById(R.id.registereditText5);
	        
	        register =(Button)findViewById(R.id.registerbutton1);
	        back =(TextView)findViewById(R.id.link_to_login);
	        
	        register.setOnClickListener(this);
	        back.setOnClickListener(this);
	        
	        
	 }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
    	{
    		case R.id.registerbutton1:
        		dialog = ProgressDialog.show(this, "", 
                        "Loading. Please wait...", true);

        		new Thread(new Runnable() {
      			  public void run() {
    			cookies = sendPostData("http://smartwebvina.com/smartlive/index.php?option=com_user&task=register",
    					editname.getText().toString(),
    					editusername.getText().toString(), 
    					editemail.getText().toString(),
        				editpasswd.getText().toString(),
        				editrepasswd.getText().toString());
    			handler.sendEmptyMessage(0);
      			  }
        		}).start();
    			break;
    			
    		case R.id.link_to_login:
    			Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        		startActivity(intent);
        		break;
    	}
		
	}
	public List<Cookie> sendPostData(String url,String name, String user,String email, String pass,String repass) {

		   // Setup a HTTP client, HttpPost (that contains data you wanna send) and
		   // a HttpResponse that gonna catch a response.
		   DefaultHttpClient postClient = new DefaultHttpClient();
		   HttpPost httpPost = new HttpPost(url);
	
		   HttpResponse response;
		   List<Cookie> cookies = null;
		  
		

		   try {   
			  
		      // Make a List. Increase the size as you wish.
		      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		      // Add your form name and a text that belongs to the actual form.
		      
		      nameValuePairs.add(new BasicNameValuePair("name", name));
		      nameValuePairs.add(new BasicNameValuePair("username", user));
		      nameValuePairs.add(new BasicNameValuePair("email", email));
		      nameValuePairs.add(new BasicNameValuePair("password", pass));
		      nameValuePairs.add(new BasicNameValuePair("password2", repass));
		
		    //  nameValuePairs.add(new BasicNameValuePair("remember", "yes"));
		      

		      nameValuePairs.add(new BasicNameValuePair("task", "register_save"));
		      nameValuePairs.add(new BasicNameValuePair("id", "0"));
		      nameValuePairs.add(new BasicNameValuePair("gid", "0"));
		     nameValuePairs.add(new BasicNameValuePair("f6f6cb580925b9a2a5f5d2e83482456e", "1"));

		      // Set the entity of your HttpPost.
		      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

		      // Execute your request against the given url and catch the response.
		      response = postClient.execute(httpPost);
		    
		      
		      // Status code 200 == successfully posted data.
		      if(response.getStatusLine().getStatusCode() == 200) {
		         // Green light. Catch your cookies from your HTTP response.
		          cookies =  postClient.getCookieStore().getCookies(); 
		         return  cookies;
		      }
		   } catch (Exception e) {
		   }
		   return  cookies;
	
	}
 
 

}
